const app = getApp()
const util = require('../../utils/util.js')

const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {
    content: '',
    showVal: '', //展示出来的物品标题
    showList: [],
    resArr: [{
      title: '啥也不说了不买就等着哭吧啥也不说了不买就等着哭吧啥也不说了不买就哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈'
    }, {
      title: '啥也不说了不买就等着哭吧啥也不说了不买就等着哭吧啥也不说了不买就哈哈哈哈哈哈哈哈哈ghehehehehhe'
    }],
    penIdx: -1, //点击的第几个笔 默认-1
    hiddenShow: false,
    textDisable: false,
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    authorization: true,
    showPhoto: false,
   
    permissionSetting: {
      setingOn: false,
      save: true,
      typeShare: 1,
    }
  },
  getVal(e) { //获取富文本框value 
    var val = e.detail.value;
    this.setData({
      content: val
    })
  },
  hide(e) {
    let penIdx = e.currentTarget.dataset.idx, //点击的是第几个笔
      goods_id = e.currentTarget.dataset.goods_id //点击的goods_id
    this.setData({
      content:'',
      goods_id,
      hiddenShow: true,
      penIdx: penIdx,
      textDisable: false, //富文本可输入
    })
  },
  onLoad: function(options) {
    var that = this
    wx.setNavigationBarTitle({
      title: options.Ta ? '他选的货' : '我选的货',
    })

    this.setData({  
      showList: this.data.resArr,
      parent_id: options.Ta ? options.Ta : false
    })
    console.log(this.data.parent_id)
  },
  onShow(){
    this.showList()
  },
  // myevent(e) {
  //   var that = this
  //   that.setData({
  //     permissionSetting: e.detail.permissionSetting
  //   })
  // },

  showList() {
    var that = this
    util.API('pyapi/my_chose_goods_list', {
      parent_id: that.data.parent_id ? that.data.parent_id : 0
    }, 'application/x-www-form-urlencoded').then(res => {
      console.log(res)
      let {
        code,
        data,
        member_info,
        msg
      } = res
      data = data.map(item => {
        // if (item.goods__goods_cover.indexOf(",") != -1) {
          item.goods__goods_cover = item.goods__goods_cover.split(',')
        // }
        return item
      })
      that.setData({
        data: data,
        member_info,
        'permissionSetting.data': data
      })
      console.log(data,'asdasd')
      getApp().authorization().then(res => {
        that.setData({
          authorization: res,
        })
      })
    })
  },
  tapFootBtn(e) {
    let type = e.currentTarget.dataset.status,
      goods_id = e.currentTarget.dataset.goods_id,
      idx = e.currentTarget.dataset.idx
    util.API('pyapi/add_my_chose_goods', {
      type,
      goods_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        code,
        data,
        msg
      } = res
      let that = this
      if (code == 200) {
        wx.showToast({
          title: msg + '',
          icon: 'none',
          success() {
            if (type == 'top') {
              let spliceItem = that.data.data.splice(idx, 1)
              that.data.data.unshift(spliceItem[0])
              that.setData({
                data: that.data.data,
                'permissionSetting.data': that.data.data
              })
           

            } else if (type == 'del') {
              that.data.data.splice(idx, 1)
              that.setData({
                data: that.data.data,
                'permissionSetting.data': that.data.data
              })
            }
          }
        })
      }
    })
  },
  changeInfo(e) {
    let goods_id = this.data.goods_id
    var e = e.currentTarget.dataset.show
    if (e == 0) {
    
      util.API('pyapi/add_my_chose_goods', {
        type: 'edit',
        goods_id,
        content: this.data.content
      }, 'application/x-www-form-urlencoded').then(res => {
        let {
          code,
          data,
          msg
        } = res
        if (code == 200) {
          let idx = this.data.penIdx
          this.data.data[idx].recommond_content = this.data.content
          this.setData({
            data: this.data.data,
            hiddenShow: false
          })
        }
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      })
    } else {
      this.setData({
        hiddenShow: false
      })
    }
  },
  jumpSPXQ(e) {
    let goods_id = e.currentTarget.dataset.goods_id
    wx, wx.navigateTo({
      url: `/pages/shopDetails/shopDetails?goods_id=${goods_id}&parent_id=${this.data.parent_id}`

    })
  },
  getUserInfo: function(e) {
    if (e.detail.userInfo) {
      this.setData({
        authorization: e.detail.userInfo,
        showPhoto: true,
        'permissionSetting.data': this.data.data
      })
    }

  },

  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onShareAppMessage(e) {
    var shareimage = getApp().globalData.shareimage
    shareimage ? shareimage = shareimage.tempFilePath : ""
    this.setData({
      shareimage
    })
    return {
      title: getApp().globalData.shopname,
      path: 'pages/index/index?scene=normal' + `&member_id=${getApp().globalData.member_id}&type=1`,
      imageUrl: shareimage,
      success: (res) => { // 成功后要做的事情   
        wx.showToast({
          title: '转发成功',
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '转发失败',
        })
      }
    }
  },
  onReachBottom: function() {},
  /**
   * 用户点击右上角分享
   */

}))